<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>数据加密标准（DES）的C++语言描述实现 | 夜悊的技术小宅</title><meta name="author" content="夜悊"><meta name="copyright" content="夜悊"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="前言网络安全中数据加密标准（DES）的C++语言描述实现。  代码仓库 yezhening&#x2F;Programming-examples: 编程实例 (github.com) Programming-examples: 编程实例 (gitee.com)   代码特点纯C++语言：  相对规范和整洁 一定程度地面向对象 使用一部分高级特性 考虑优化性能  详细注释：  提示规范和整洁 提示面向">
<meta property="og:type" content="article">
<meta property="og:title" content="数据加密标准（DES）的C++语言描述实现">
<meta property="og:url" content="http://example.com/2022/10/24/%E6%95%B0%E6%8D%AE%E5%8A%A0%E5%AF%86%E6%A0%87%E5%87%86%EF%BC%88DES%EF%BC%89%E7%9A%84C++%E8%AF%AD%E8%A8%80%E6%8F%8F%E8%BF%B0%E5%AE%9E%E7%8E%B0/index.html">
<meta property="og:site_name" content="夜悊的技术小宅">
<meta property="og:description" content="前言网络安全中数据加密标准（DES）的C++语言描述实现。  代码仓库 yezhening&#x2F;Programming-examples: 编程实例 (github.com) Programming-examples: 编程实例 (gitee.com)   代码特点纯C++语言：  相对规范和整洁 一定程度地面向对象 使用一部分高级特性 考虑优化性能  详细注释：  提示规范和整洁 提示面向">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/img/cover10.png">
<meta property="article:published_time" content="2022-10-24T12:19:59.000Z">
<meta property="article:modified_time" content="2023-04-26T07:44:57.506Z">
<meta property="article:author" content="夜悊">
<meta property="article:tag" content="网络安全">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/img/cover10.png"><link rel="shortcut icon" href="/img/favicon2.png"><link rel="canonical" href="http://example.com/2022/10/24/%E6%95%B0%E6%8D%AE%E5%8A%A0%E5%AF%86%E6%A0%87%E5%87%86%EF%BC%88DES%EF%BC%89%E7%9A%84C++%E8%AF%AD%E8%A8%80%E6%8F%8F%E8%BF%B0%E5%AE%9E%E7%8E%B0/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css?v=4.13.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.5.1/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: 夜悊","link":"链接: ","source":"来源: 夜悊的技术小宅","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid@4.11.1/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '数据加密标准（DES）的C++语言描述实现',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-04-26 15:44:57'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><link rel="stylesheet" href="/css/my_css.css"><meta name="generator" content="Hexo 7.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">92</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">15</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 笔记目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-blog"></i><span> 其他博客</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://blog.csdn.net/m0_62083249"><i class="fa-fw fa fa-link"></i><span> CSDN</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.zhihu.com/people/ye-zhe-ning"><i class="fa-fw fa fa-link"></i><span> 知乎</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-sitemap"></i><span> 代码仓库</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://gitee.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Gitee</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://github.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Github</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about_me/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/cover10.png')"><nav id="nav"><span id="blog-info"><a href="/" title="夜悊的技术小宅"><span class="site-name">夜悊的技术小宅</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 笔记目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-blog"></i><span> 其他博客</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://blog.csdn.net/m0_62083249"><i class="fa-fw fa fa-link"></i><span> CSDN</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.zhihu.com/people/ye-zhe-ning"><i class="fa-fw fa fa-link"></i><span> 知乎</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-sitemap"></i><span> 代码仓库</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://gitee.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Gitee</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://github.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Github</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about_me/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">数据加密标准（DES）的C++语言描述实现</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-10-24T12:19:59.000Z" title="发表于 2022-10-24 20:19:59">2022-10-24</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-04-26T07:44:57.506Z" title="更新于 2023-04-26 15:44:57">2023-04-26</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%BD%91%E7%BB%9C%E5%AE%89%E5%85%A8/">网络安全</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="数据加密标准（DES）的C++语言描述实现"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>网络安全中数据加密标准（DES）的C++语言描述实现。</p>
<hr>
<h1 id="代码仓库"><a href="#代码仓库" class="headerlink" title="代码仓库"></a>代码仓库</h1><ul>
<li><a target="_blank" rel="noopener" href="https://github.com/yezhening/Programming-examples">yezhening&#x2F;Programming-examples: 编程实例 (github.com)</a></li>
<li><a target="_blank" rel="noopener" href="https://gitee.com/yezhening/Programming-examples">Programming-examples: 编程实例 (gitee.com)</a></li>
</ul>
<hr>
<h1 id="代码特点"><a href="#代码特点" class="headerlink" title="代码特点"></a>代码特点</h1><p>纯C++语言：</p>
<ul>
<li>相对规范和整洁</li>
<li>一定程度地面向对象</li>
<li>使用一部分高级特性</li>
<li>考虑优化性能</li>
</ul>
<p>详细注释：</p>
<ul>
<li>提示规范和整洁</li>
<li>提示面向对象</li>
<li>提示高级特性</li>
<li>提示优化性能</li>
<li>解析数据加密标准（DES）步骤</li>
<li>注意易错点</li>
</ul>
<hr>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><h2 id="des-h"><a href="#des-h" class="headerlink" title="des.h"></a>des.h</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//预处理指令——————————</span></span><br><span class="line"><span class="comment">//预定义宏</span></span><br><span class="line"><span class="meta">#<span class="keyword">ifndef</span> DES_DES_H_</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> DES_DES_H_</span></span><br><span class="line"><span class="comment">//提示：防止头文件被重复包含</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//标准库头文件</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span> <span class="comment">//string，cout，endl</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span>   <span class="comment">//vector</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//命名空间</span></span><br><span class="line"><span class="comment">//提示：</span></span><br><span class="line"><span class="comment">//使用using声明而不是using指示，以体现命名空间的作用</span></span><br><span class="line"><span class="comment">//本项目并未体现命名空间的作用，因为只使用一个命名空间std</span></span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"><span class="keyword">using</span> std::string;</span><br><span class="line"><span class="keyword">using</span> std::vector;</span><br><span class="line"></span><br><span class="line"><span class="comment">//自定义数据类型——————————</span></span><br><span class="line"><span class="comment">//类</span></span><br><span class="line"><span class="comment">//数据加密标准类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">DES</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">encrypt</span><span class="params">(<span class="type">const</span> string &amp;plainText, <span class="type">const</span> string &amp;key, string &amp;cipherTextASCII)</span></span>;      <span class="comment">//加密</span></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">decrypt</span><span class="params">(<span class="type">const</span> string &amp;cipherTextASCII, <span class="type">const</span> string &amp;key, string &amp;plainTextASCII)</span></span>; <span class="comment">//解密</span></span><br><span class="line">    <span class="comment">//提示：</span></span><br><span class="line">    <span class="comment">//加密和解密过程相似，存在大量代码复用</span></span><br><span class="line">    <span class="comment">//但仍依据语义拆分成两函数，解耦合以能够单独调用</span></span><br><span class="line">    <span class="comment">//提示：返回值的数据类型为void而不是string，和其他函数形式统一，反正结果都会输出</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="comment">//提示：属性初始化，避免未显式定义的默认构造函数调用造成未知错误</span></span><br><span class="line">    <span class="comment">//提示：因为会修改属性，所以不使用const修饰</span></span><br><span class="line">    <span class="comment">//提示：记录二进制而不是字符是数据加密标准的原貌，更有实际意义</span></span><br><span class="line">    string plainTextASCII&#123;<span class="string">&quot;&quot;</span>&#125;;  <span class="comment">//明文的ASCII码  64位</span></span><br><span class="line">    string keyASCII&#123;<span class="string">&quot;&quot;</span>&#125;;        <span class="comment">//密钥的ASCII码  64位</span></span><br><span class="line">    string cipherTextASCII&#123;<span class="string">&quot;&quot;</span>&#125;; <span class="comment">//密文的ASCII码 64位</span></span><br><span class="line"></span><br><span class="line">    vector&lt;string&gt; wheelKeys&#123;</span><br><span class="line">        <span class="string">&quot;&quot;</span>, <span class="string">&quot;&quot;</span>, <span class="string">&quot;&quot;</span>, <span class="string">&quot;&quot;</span>,</span><br><span class="line">        <span class="string">&quot;&quot;</span>, <span class="string">&quot;&quot;</span>, <span class="string">&quot;&quot;</span>, <span class="string">&quot;&quot;</span>,</span><br><span class="line">        <span class="string">&quot;&quot;</span>, <span class="string">&quot;&quot;</span>, <span class="string">&quot;&quot;</span>, <span class="string">&quot;&quot;</span>,</span><br><span class="line">        <span class="string">&quot;&quot;</span>, <span class="string">&quot;&quot;</span>, <span class="string">&quot;&quot;</span>, <span class="string">&quot;&quot;</span>&#125;; <span class="comment">//轮密钥   16个，每个56位</span></span><br><span class="line">    <span class="comment">//注意：</span></span><br><span class="line">    <span class="comment">// vector&lt;string&gt; wheelKeys(16,&quot;&quot;);报错</span></span><br><span class="line">    <span class="comment">// string默认为空字符串，可不初始化</span></span><br><span class="line">    <span class="comment">//可初始化，和其他属性初始化方式统一</span></span><br><span class="line">    <span class="comment">//提示：记录轮密钥，可将轮密钥生成过程和轮函数过程解耦合，可提前准备轮密钥</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//提示：将固有属性放在类中，而不是初始化为静态全局变量</span></span><br><span class="line">    <span class="comment">//提示：C++11新特性，可用大括号初始化任何类型</span></span><br><span class="line">    <span class="comment">//初始置换IP表</span></span><br><span class="line">    <span class="type">const</span> string ipTable&#123;</span><br><span class="line">        <span class="number">58</span>, <span class="number">50</span>, <span class="number">42</span>, <span class="number">34</span>, <span class="number">26</span>, <span class="number">18</span>, <span class="number">10</span>, <span class="number">2</span>,</span><br><span class="line">        <span class="number">60</span>, <span class="number">52</span>, <span class="number">44</span>, <span class="number">36</span>, <span class="number">28</span>, <span class="number">20</span>, <span class="number">12</span>, <span class="number">4</span>,</span><br><span class="line">        <span class="number">62</span>, <span class="number">54</span>, <span class="number">46</span>, <span class="number">38</span>, <span class="number">30</span>, <span class="number">22</span>, <span class="number">14</span>, <span class="number">6</span>,</span><br><span class="line">        <span class="number">64</span>, <span class="number">56</span>, <span class="number">48</span>, <span class="number">40</span>, <span class="number">32</span>, <span class="number">24</span>, <span class="number">16</span>, <span class="number">8</span>,</span><br><span class="line">        <span class="number">57</span>, <span class="number">49</span>, <span class="number">41</span>, <span class="number">33</span>, <span class="number">25</span>, <span class="number">17</span>, <span class="number">9</span>, <span class="number">1</span>,</span><br><span class="line">        <span class="number">59</span>, <span class="number">51</span>, <span class="number">43</span>, <span class="number">35</span>, <span class="number">27</span>, <span class="number">19</span>, <span class="number">11</span>, <span class="number">3</span>,</span><br><span class="line">        <span class="number">61</span>, <span class="number">53</span>, <span class="number">45</span>, <span class="number">37</span>, <span class="number">29</span>, <span class="number">21</span>, <span class="number">13</span>, <span class="number">5</span>,</span><br><span class="line">        <span class="number">63</span>, <span class="number">55</span>, <span class="number">47</span>, <span class="number">39</span>, <span class="number">31</span>, <span class="number">23</span>, <span class="number">15</span>, <span class="number">7</span>&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//密钥的选择置换表</span></span><br><span class="line">    <span class="type">const</span> string keySeRepTable&#123;</span><br><span class="line">        <span class="number">57</span>, <span class="number">49</span>, <span class="number">41</span>, <span class="number">33</span>, <span class="number">25</span>, <span class="number">17</span>, <span class="number">9</span>,</span><br><span class="line">        <span class="number">1</span>, <span class="number">58</span>, <span class="number">50</span>, <span class="number">42</span>, <span class="number">34</span>, <span class="number">26</span>, <span class="number">18</span>,</span><br><span class="line">        <span class="number">10</span>, <span class="number">2</span>, <span class="number">59</span>, <span class="number">51</span>, <span class="number">43</span>, <span class="number">35</span>, <span class="number">27</span>,</span><br><span class="line">        <span class="number">9</span>, <span class="number">11</span>, <span class="number">3</span>, <span class="number">60</span>, <span class="number">52</span>, <span class="number">44</span>, <span class="number">36</span>,</span><br><span class="line">        <span class="number">63</span>, <span class="number">55</span>, <span class="number">47</span>, <span class="number">39</span>, <span class="number">31</span>, <span class="number">23</span>, <span class="number">15</span>,</span><br><span class="line">        <span class="number">7</span>, <span class="number">62</span>, <span class="number">54</span>, <span class="number">46</span>, <span class="number">38</span>, <span class="number">30</span>, <span class="number">22</span>,</span><br><span class="line">        <span class="number">14</span>, <span class="number">6</span>, <span class="number">61</span>, <span class="number">53</span>, <span class="number">45</span>, <span class="number">37</span>, <span class="number">29</span>,</span><br><span class="line">        <span class="number">21</span>, <span class="number">13</span>, <span class="number">5</span>, <span class="number">28</span>, <span class="number">20</span>, <span class="number">12</span>, <span class="number">4</span>&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//密钥的左移位表</span></span><br><span class="line">    <span class="type">const</span> vector&lt;<span class="type">int</span>&gt; keyLeftMoveTable&#123;<span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">1</span>&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//密钥的压缩置换表</span></span><br><span class="line">    <span class="type">const</span> string keyComRepTable&#123;</span><br><span class="line">        <span class="number">14</span>, <span class="number">17</span>, <span class="number">11</span>, <span class="number">24</span>, <span class="number">1</span>, <span class="number">5</span>,</span><br><span class="line">        <span class="number">3</span>, <span class="number">28</span>, <span class="number">15</span>, <span class="number">6</span>, <span class="number">21</span>, <span class="number">10</span>,</span><br><span class="line">        <span class="number">23</span>, <span class="number">19</span>, <span class="number">12</span>, <span class="number">4</span>, <span class="number">26</span>, <span class="number">8</span>,</span><br><span class="line">        <span class="number">16</span>, <span class="number">7</span>, <span class="number">27</span>, <span class="number">20</span>, <span class="number">13</span>, <span class="number">2</span>,</span><br><span class="line">        <span class="number">41</span>, <span class="number">52</span>, <span class="number">31</span>, <span class="number">37</span>, <span class="number">47</span>, <span class="number">55</span>,</span><br><span class="line">        <span class="number">30</span>, <span class="number">40</span>, <span class="number">51</span>, <span class="number">45</span>, <span class="number">33</span>, <span class="number">48</span>,</span><br><span class="line">        <span class="number">44</span>, <span class="number">49</span>, <span class="number">39</span>, <span class="number">56</span>, <span class="number">34</span>, <span class="number">53</span>,</span><br><span class="line">        <span class="number">46</span>, <span class="number">42</span>, <span class="number">50</span>, <span class="number">36</span>, <span class="number">29</span>, <span class="number">32</span>&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//扩展置换E表</span></span><br><span class="line">    <span class="type">const</span> string eRepTable&#123;</span><br><span class="line">        <span class="number">32</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>,</span><br><span class="line">        <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>,</span><br><span class="line">        <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>,</span><br><span class="line">        <span class="number">12</span>, <span class="number">13</span>, <span class="number">14</span>, <span class="number">15</span>, <span class="number">16</span>, <span class="number">17</span>,</span><br><span class="line">        <span class="number">16</span>, <span class="number">17</span>, <span class="number">18</span>, <span class="number">19</span>, <span class="number">20</span>, <span class="number">21</span>,</span><br><span class="line">        <span class="number">20</span>, <span class="number">21</span>, <span class="number">22</span>, <span class="number">23</span>, <span class="number">24</span>, <span class="number">25</span>,</span><br><span class="line">        <span class="number">24</span>, <span class="number">25</span>, <span class="number">26</span>, <span class="number">27</span>, <span class="number">28</span>, <span class="number">29</span>,</span><br><span class="line">        <span class="number">28</span>, <span class="number">29</span>, <span class="number">30</span>, <span class="number">31</span>, <span class="number">32</span>, <span class="number">1</span>&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// S盒选择代替表</span></span><br><span class="line">    <span class="type">const</span> vector&lt;string&gt; sBoxSeRepTable&#123;</span><br><span class="line">        <span class="comment">// S1</span></span><br><span class="line">        &#123;<span class="number">14</span>, <span class="number">4</span>, <span class="number">13</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">15</span>, <span class="number">11</span>, <span class="number">8</span>, <span class="number">3</span>, <span class="number">10</span>, <span class="number">6</span>, <span class="number">12</span>, <span class="number">5</span>, <span class="number">9</span>, <span class="number">0</span>, <span class="number">7</span>,</span><br><span class="line">         <span class="number">0</span>, <span class="number">15</span>, <span class="number">7</span>, <span class="number">4</span>, <span class="number">14</span>, <span class="number">2</span>, <span class="number">13</span>, <span class="number">1</span>, <span class="number">10</span>, <span class="number">6</span>, <span class="number">12</span>, <span class="number">11</span>, <span class="number">9</span>, <span class="number">5</span>, <span class="number">3</span>, <span class="number">8</span>,</span><br><span class="line">         <span class="number">4</span>, <span class="number">1</span>, <span class="number">14</span>, <span class="number">8</span>, <span class="number">13</span>, <span class="number">6</span>, <span class="number">2</span>, <span class="number">11</span>, <span class="number">15</span>, <span class="number">12</span>, <span class="number">9</span>, <span class="number">7</span>, <span class="number">3</span>, <span class="number">10</span>, <span class="number">5</span>, <span class="number">0</span>,</span><br><span class="line">         <span class="number">15</span>, <span class="number">12</span>, <span class="number">8</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">9</span>, <span class="number">1</span>, <span class="number">7</span>, <span class="number">5</span>, <span class="number">11</span>, <span class="number">3</span>, <span class="number">14</span>, <span class="number">10</span>, <span class="number">0</span>, <span class="number">6</span>, <span class="number">13</span>&#125;,</span><br><span class="line"></span><br><span class="line">        <span class="comment">// S2</span></span><br><span class="line">        &#123;<span class="number">15</span>, <span class="number">1</span>, <span class="number">8</span>, <span class="number">14</span>, <span class="number">6</span>, <span class="number">11</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">9</span>, <span class="number">7</span>, <span class="number">2</span>, <span class="number">13</span>, <span class="number">12</span>, <span class="number">0</span>, <span class="number">5</span>, <span class="number">10</span>,</span><br><span class="line">         <span class="number">3</span>, <span class="number">13</span>, <span class="number">4</span>, <span class="number">7</span>, <span class="number">15</span>, <span class="number">2</span>, <span class="number">8</span>, <span class="number">14</span>, <span class="number">12</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">10</span>, <span class="number">6</span>, <span class="number">9</span>, <span class="number">11</span>, <span class="number">5</span>,</span><br><span class="line">         <span class="number">0</span>, <span class="number">14</span>, <span class="number">7</span>, <span class="number">11</span>, <span class="number">10</span>, <span class="number">4</span>, <span class="number">13</span>, <span class="number">1</span>, <span class="number">5</span>, <span class="number">8</span>, <span class="number">12</span>, <span class="number">6</span>, <span class="number">9</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">15</span>,</span><br><span class="line">         <span class="number">13</span>, <span class="number">8</span>, <span class="number">10</span>, <span class="number">1</span>, <span class="number">3</span>, <span class="number">15</span>, <span class="number">4</span>, <span class="number">2</span>, <span class="number">11</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">12</span>, <span class="number">0</span>, <span class="number">5</span>, <span class="number">14</span>, <span class="number">9</span>&#125;,</span><br><span class="line"></span><br><span class="line">        <span class="comment">// S3</span></span><br><span class="line">        &#123;<span class="number">10</span>, <span class="number">0</span>, <span class="number">9</span>, <span class="number">14</span>, <span class="number">6</span>, <span class="number">3</span>, <span class="number">15</span>, <span class="number">5</span>, <span class="number">1</span>, <span class="number">13</span>, <span class="number">12</span>, <span class="number">7</span>, <span class="number">11</span>, <span class="number">4</span>, <span class="number">2</span>, <span class="number">8</span>,</span><br><span class="line">         <span class="number">13</span>, <span class="number">7</span>, <span class="number">0</span>, <span class="number">9</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">10</span>, <span class="number">2</span>, <span class="number">8</span>, <span class="number">5</span>, <span class="number">14</span>, <span class="number">12</span>, <span class="number">11</span>, <span class="number">15</span>, <span class="number">1</span>,</span><br><span class="line">         <span class="number">13</span>, <span class="number">6</span>, <span class="number">4</span>, <span class="number">9</span>, <span class="number">8</span>, <span class="number">15</span>, <span class="number">3</span>, <span class="number">0</span>, <span class="number">11</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">12</span>, <span class="number">5</span>, <span class="number">10</span>, <span class="number">14</span>, <span class="number">7</span>,</span><br><span class="line">         <span class="number">1</span>, <span class="number">10</span>, <span class="number">13</span>, <span class="number">0</span>, <span class="number">6</span>, <span class="number">9</span>, <span class="number">8</span>, <span class="number">7</span>, <span class="number">4</span>, <span class="number">15</span>, <span class="number">14</span>, <span class="number">3</span>, <span class="number">11</span>, <span class="number">5</span>, <span class="number">2</span>, <span class="number">12</span>&#125;,</span><br><span class="line"></span><br><span class="line">        <span class="comment">// S4</span></span><br><span class="line">        &#123;<span class="number">7</span>, <span class="number">13</span>, <span class="number">14</span>, <span class="number">3</span>, <span class="number">0</span>, <span class="number">6</span>, <span class="number">9</span>, <span class="number">10</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">8</span>, <span class="number">5</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">4</span>, <span class="number">15</span>,</span><br><span class="line">         <span class="number">13</span>, <span class="number">8</span>, <span class="number">11</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">15</span>, <span class="number">0</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">7</span>, <span class="number">2</span>, <span class="number">12</span>, <span class="number">1</span>, <span class="number">10</span>, <span class="number">14</span>, <span class="number">9</span>,</span><br><span class="line">         <span class="number">10</span>, <span class="number">6</span>, <span class="number">9</span>, <span class="number">0</span>, <span class="number">12</span>, <span class="number">11</span>, <span class="number">7</span>, <span class="number">13</span>, <span class="number">15</span>, <span class="number">1</span>, <span class="number">3</span>, <span class="number">14</span>, <span class="number">5</span>, <span class="number">2</span>, <span class="number">8</span>, <span class="number">4</span>,</span><br><span class="line">         <span class="number">3</span>, <span class="number">15</span>, <span class="number">0</span>, <span class="number">6</span>, <span class="number">10</span>, <span class="number">1</span>, <span class="number">13</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">7</span>, <span class="number">2</span>, <span class="number">14</span>&#125;,</span><br><span class="line"></span><br><span class="line">        <span class="comment">// S5</span></span><br><span class="line">        &#123;<span class="number">2</span>, <span class="number">12</span>, <span class="number">4</span>, <span class="number">1</span>, <span class="number">7</span>, <span class="number">10</span>, <span class="number">11</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">5</span>, <span class="number">3</span>, <span class="number">15</span>, <span class="number">13</span>, <span class="number">0</span>, <span class="number">14</span>, <span class="number">9</span>,</span><br><span class="line">         <span class="number">14</span>, <span class="number">11</span>, <span class="number">2</span>, <span class="number">12</span>, <span class="number">4</span>, <span class="number">7</span>, <span class="number">13</span>, <span class="number">1</span>, <span class="number">5</span>, <span class="number">0</span>, <span class="number">15</span>, <span class="number">10</span>, <span class="number">3</span>, <span class="number">9</span>, <span class="number">8</span>, <span class="number">6</span>,</span><br><span class="line">         <span class="number">4</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">11</span>, <span class="number">10</span>, <span class="number">13</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">15</span>, <span class="number">9</span>, <span class="number">12</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">3</span>, <span class="number">0</span>, <span class="number">14</span>,</span><br><span class="line">         <span class="number">11</span>, <span class="number">8</span>, <span class="number">12</span>, <span class="number">7</span>, <span class="number">1</span>, <span class="number">14</span>, <span class="number">2</span>, <span class="number">13</span>, <span class="number">6</span>, <span class="number">15</span>, <span class="number">0</span>, <span class="number">9</span>, <span class="number">10</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">3</span>&#125;,</span><br><span class="line"></span><br><span class="line">        <span class="comment">// S6</span></span><br><span class="line">        &#123;<span class="number">12</span>, <span class="number">1</span>, <span class="number">10</span>, <span class="number">15</span>, <span class="number">9</span>, <span class="number">2</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">0</span>, <span class="number">13</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">14</span>, <span class="number">7</span>, <span class="number">5</span>, <span class="number">11</span>,</span><br><span class="line">         <span class="number">10</span>, <span class="number">15</span>, <span class="number">4</span>, <span class="number">2</span>, <span class="number">7</span>, <span class="number">12</span>, <span class="number">9</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">13</span>, <span class="number">14</span>, <span class="number">0</span>, <span class="number">11</span>, <span class="number">3</span>, <span class="number">8</span>,</span><br><span class="line">         <span class="number">9</span>, <span class="number">14</span>, <span class="number">15</span>, <span class="number">5</span>, <span class="number">2</span>, <span class="number">8</span>, <span class="number">12</span>, <span class="number">3</span>, <span class="number">7</span>, <span class="number">0</span>, <span class="number">4</span>, <span class="number">10</span>, <span class="number">1</span>, <span class="number">13</span>, <span class="number">11</span>, <span class="number">6</span>,</span><br><span class="line">         <span class="number">4</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">12</span>, <span class="number">9</span>, <span class="number">5</span>, <span class="number">15</span>, <span class="number">10</span>, <span class="number">11</span>, <span class="number">14</span>, <span class="number">1</span>, <span class="number">7</span>, <span class="number">6</span>, <span class="number">0</span>, <span class="number">8</span>, <span class="number">13</span>&#125;,</span><br><span class="line"></span><br><span class="line">        <span class="comment">// S7</span></span><br><span class="line">        &#123;<span class="number">4</span>, <span class="number">11</span>, <span class="number">2</span>, <span class="number">14</span>, <span class="number">15</span>, <span class="number">0</span>, <span class="number">8</span>, <span class="number">13</span>, <span class="number">3</span>, <span class="number">12</span>, <span class="number">9</span>, <span class="number">7</span>, <span class="number">5</span>, <span class="number">10</span>, <span class="number">6</span>, <span class="number">1</span>,</span><br><span class="line">         <span class="number">13</span>, <span class="number">0</span>, <span class="number">11</span>, <span class="number">7</span>, <span class="number">4</span>, <span class="number">9</span>, <span class="number">1</span>, <span class="number">10</span>, <span class="number">14</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">12</span>, <span class="number">2</span>, <span class="number">15</span>, <span class="number">8</span>, <span class="number">6</span>,</span><br><span class="line">         <span class="number">1</span>, <span class="number">4</span>, <span class="number">11</span>, <span class="number">13</span>, <span class="number">12</span>, <span class="number">3</span>, <span class="number">7</span>, <span class="number">14</span>, <span class="number">10</span>, <span class="number">15</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">0</span>, <span class="number">5</span>, <span class="number">9</span>, <span class="number">2</span>,</span><br><span class="line">         <span class="number">6</span>, <span class="number">11</span>, <span class="number">13</span>, <span class="number">8</span>, <span class="number">1</span>, <span class="number">4</span>, <span class="number">10</span>, <span class="number">7</span>, <span class="number">9</span>, <span class="number">5</span>, <span class="number">0</span>, <span class="number">15</span>, <span class="number">14</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">12</span>&#125;,</span><br><span class="line"></span><br><span class="line">        <span class="comment">// S8</span></span><br><span class="line">        &#123;<span class="number">13</span>, <span class="number">2</span>, <span class="number">8</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">15</span>, <span class="number">11</span>, <span class="number">1</span>, <span class="number">10</span>, <span class="number">9</span>, <span class="number">3</span>, <span class="number">14</span>, <span class="number">5</span>, <span class="number">0</span>, <span class="number">12</span>, <span class="number">7</span>,</span><br><span class="line">         <span class="number">1</span>, <span class="number">15</span>, <span class="number">13</span>, <span class="number">8</span>, <span class="number">10</span>, <span class="number">3</span>, <span class="number">7</span>, <span class="number">4</span>, <span class="number">12</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">11</span>, <span class="number">0</span>, <span class="number">14</span>, <span class="number">9</span>, <span class="number">2</span>,</span><br><span class="line">         <span class="number">7</span>, <span class="number">11</span>, <span class="number">4</span>, <span class="number">1</span>, <span class="number">9</span>, <span class="number">12</span>, <span class="number">14</span>, <span class="number">2</span>, <span class="number">0</span>, <span class="number">6</span>, <span class="number">10</span>, <span class="number">13</span>, <span class="number">15</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">8</span>,</span><br><span class="line">         <span class="number">2</span>, <span class="number">1</span>, <span class="number">14</span>, <span class="number">7</span>, <span class="number">4</span>, <span class="number">10</span>, <span class="number">8</span>, <span class="number">13</span>, <span class="number">15</span>, <span class="number">12</span>, <span class="number">9</span>, <span class="number">0</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">11</span>&#125;&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 置换P表</span></span><br><span class="line">    <span class="type">const</span> string pRepTable&#123;</span><br><span class="line">        <span class="number">16</span>, <span class="number">7</span>, <span class="number">20</span>, <span class="number">21</span>,</span><br><span class="line">        <span class="number">29</span>, <span class="number">12</span>, <span class="number">28</span>, <span class="number">17</span>,</span><br><span class="line">        <span class="number">1</span>, <span class="number">15</span>, <span class="number">23</span>, <span class="number">26</span>,</span><br><span class="line">        <span class="number">5</span>, <span class="number">18</span>, <span class="number">31</span>, <span class="number">10</span>,</span><br><span class="line">        <span class="number">2</span>, <span class="number">8</span>, <span class="number">24</span>, <span class="number">14</span>,</span><br><span class="line">        <span class="number">32</span>, <span class="number">27</span>, <span class="number">3</span>, <span class="number">9</span>,</span><br><span class="line">        <span class="number">19</span>, <span class="number">13</span>, <span class="number">30</span>, <span class="number">6</span>,</span><br><span class="line">        <span class="number">22</span>, <span class="number">11</span>, <span class="number">4</span>, <span class="number">25</span>&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//逆初始置换IP-1表</span></span><br><span class="line">    <span class="type">const</span> string iIpTable&#123;</span><br><span class="line">        <span class="number">40</span>, <span class="number">8</span>, <span class="number">48</span>, <span class="number">16</span>, <span class="number">56</span>, <span class="number">24</span>, <span class="number">64</span>, <span class="number">32</span>,</span><br><span class="line">        <span class="number">39</span>, <span class="number">7</span>, <span class="number">47</span>, <span class="number">15</span>, <span class="number">55</span>, <span class="number">23</span>, <span class="number">63</span>, <span class="number">31</span>,</span><br><span class="line">        <span class="number">38</span>, <span class="number">6</span>, <span class="number">46</span>, <span class="number">14</span>, <span class="number">54</span>, <span class="number">22</span>, <span class="number">62</span>, <span class="number">30</span>,</span><br><span class="line">        <span class="number">37</span>, <span class="number">5</span>, <span class="number">45</span>, <span class="number">13</span>, <span class="number">53</span>, <span class="number">21</span>, <span class="number">61</span>, <span class="number">29</span>,</span><br><span class="line">        <span class="number">36</span>, <span class="number">4</span>, <span class="number">44</span>, <span class="number">12</span>, <span class="number">52</span>, <span class="number">20</span>, <span class="number">60</span>, <span class="number">28</span>,</span><br><span class="line">        <span class="number">35</span>, <span class="number">3</span>, <span class="number">43</span>, <span class="number">11</span>, <span class="number">51</span>, <span class="number">19</span>, <span class="number">59</span>, <span class="number">27</span>,</span><br><span class="line">        <span class="number">34</span>, <span class="number">2</span>, <span class="number">42</span>, <span class="number">10</span>, <span class="number">50</span>, <span class="number">18</span>, <span class="number">58</span>, <span class="number">26</span>,</span><br><span class="line">        <span class="number">33</span>, <span class="number">1</span>, <span class="number">41</span>, <span class="number">9</span>, <span class="number">49</span>, <span class="number">17</span>, <span class="number">57</span>, <span class="number">25</span>&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//提示：使用const表示只读；使用引用传地址而不是值，避免拷贝开销-&gt;搭配使用</span></span><br><span class="line">    <span class="comment">//提示：非常量引用实参可传递给常量引用形参</span></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">byteStrToBitStr</span><span class="params">(<span class="type">const</span> string &amp;str, string &amp;bitStr)</span></span>; <span class="comment">// 8字节字符串转64位字符串 依据ASCII码</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">wheelKeyGener</span><span class="params">(<span class="type">const</span> string &amp;preKey, <span class="type">const</span> <span class="type">int</span> &amp;wheelCount, string &amp;nextKey, string &amp;wheelKey)</span></span>; <span class="comment">//轮密钥生成</span></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">wheelFunc</span><span class="params">(<span class="type">const</span> string &amp;textIp, <span class="type">const</span> string &amp;keySeRep, string &amp;textWheelF)</span></span>;                   <span class="comment">//轮函数F</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span> <span class="comment">// DES_DES_H_</span></span></span><br></pre></td></tr></table></figure>
<hr>
<h2 id="des-cpp"><a href="#des-cpp" class="headerlink" title="des.cpp"></a>des.cpp</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br><span class="line">399</span><br><span class="line">400</span><br><span class="line">401</span><br><span class="line">402</span><br><span class="line">403</span><br><span class="line">404</span><br><span class="line">405</span><br><span class="line">406</span><br><span class="line">407</span><br><span class="line">408</span><br><span class="line">409</span><br><span class="line">410</span><br><span class="line">411</span><br><span class="line">412</span><br><span class="line">413</span><br><span class="line">414</span><br><span class="line">415</span><br><span class="line">416</span><br><span class="line">417</span><br><span class="line">418</span><br><span class="line">419</span><br><span class="line">420</span><br><span class="line">421</span><br><span class="line">422</span><br><span class="line">423</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//预处理指令——————————</span></span><br><span class="line"><span class="comment">//标准库头文件</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;bitset&gt;</span>    <span class="comment">//&lt;bitset&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;algorithm&gt;</span> <span class="comment">//reverse()</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string&gt;</span>    <span class="comment">//stoi()</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//自定义头文件</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;des.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//命名空间</span></span><br><span class="line"><span class="comment">//提示：</span></span><br><span class="line"><span class="comment">//使用using声明而不是using指示，以体现命名空间的作用</span></span><br><span class="line"><span class="comment">//本项目并未体现命名空间的作用，因为只使用一个命名空间std</span></span><br><span class="line"><span class="keyword">using</span> std::bitset;</span><br><span class="line"></span><br><span class="line"><span class="comment">//加密</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">DES::encrypt</span><span class="params">(<span class="type">const</span> string &amp;plainText, <span class="type">const</span> string &amp;key, string &amp;cipherTextASCII)</span> <span class="comment">//参数：明文，密钥，密文的ASCII码</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;加密过程：————————————————————&quot;</span> &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//对明文和密钥：</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;明文：\t&quot;</span> &lt;&lt; plainText &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;密钥：\t&quot;</span> &lt;&lt; key &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 0.初始化为二进制  8字节-&gt;64位</span></span><br><span class="line">    <span class="function">string <span class="title">plainTextASCII</span><span class="params">(<span class="string">&quot;&quot;</span>)</span></span>;                        <span class="comment">//明文的ASCII码   64位</span></span><br><span class="line">    <span class="keyword">this</span>-&gt;<span class="built_in">byteStrToBitStr</span>(plainText, plainTextASCII); <span class="comment">// 8字节字符串转64位字符串 依据ASCII码</span></span><br><span class="line">    <span class="keyword">this</span>-&gt;plainTextASCII = plainTextASCII;            <span class="comment">//记录明文的ASCII码</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;明文的ASCII码：\t&quot;</span> &lt;&lt; <span class="keyword">this</span>-&gt;plainTextASCII &lt;&lt; endl;</span><br><span class="line">    <span class="function">string <span class="title">keyASCII</span><span class="params">(<span class="string">&quot;&quot;</span>)</span></span>;                  <span class="comment">//密钥的ASCII码   64位</span></span><br><span class="line">    <span class="keyword">this</span>-&gt;<span class="built_in">byteStrToBitStr</span>(key, keyASCII); <span class="comment">// 8字节字符串转64位字符串 依据ASCII码</span></span><br><span class="line">    <span class="keyword">this</span>-&gt;keyASCII = keyASCII;            <span class="comment">//记录密钥的ASCII码</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;密钥的ASCII码：\t&quot;</span> &lt;&lt; <span class="keyword">this</span>-&gt;keyASCII &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//对密钥：</span></span><br><span class="line">    <span class="comment">// 1.密钥的选择置换 64-&gt;56位</span></span><br><span class="line">    <span class="function">string <span class="title">keySeRep</span><span class="params">(<span class="number">56</span>, <span class="string">&#x27;-&#x27;</span>)</span></span>; <span class="comment">//密钥的选择置换   56位</span></span><br><span class="line">    <span class="comment">//注意：因为需要使用下标赋值，所以需要初始化大小以预分配空间</span></span><br><span class="line">    <span class="comment">//注意：初始化为不是0或1的字符，以验证是否成功</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>-&gt;keySeRepTable.<span class="built_in">size</span>(); ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//置换规则：依据置换表，输入的第57位是输出的第1位-&gt;输入的第56位是输出的第0位</span></span><br><span class="line">        <span class="comment">//注意：下标从0开始</span></span><br><span class="line">        keySeRep[i] = <span class="keyword">this</span>-&gt;keyASCII[<span class="keyword">this</span>-&gt;keySeRepTable[i] - <span class="number">1</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;密钥的选择置换：\t&quot;</span> &lt;&lt; keySeRep &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2.轮密钥生成</span></span><br><span class="line">    <span class="comment">//提示：预先生成</span></span><br><span class="line">    <span class="function">string <span class="title">preKey</span><span class="params">(keySeRep)</span></span>;  <span class="comment">//上一轮密钥 56位</span></span><br><span class="line">    <span class="function">string <span class="title">nextKey</span><span class="params">(<span class="string">&quot;&quot;</span>)</span></span>;       <span class="comment">//下一轮密钥 56位</span></span><br><span class="line">    <span class="function">string <span class="title">wheelKey</span><span class="params">(<span class="number">48</span>, <span class="string">&#x27;-&#x27;</span>)</span></span>; <span class="comment">//轮密钥 48位</span></span><br><span class="line">    <span class="comment">//注意：因为需要使用下标赋值，所以需要初始化大小以预分配空间</span></span><br><span class="line">    <span class="comment">//注意：初始化为不是0或1的字符，以验证是否成功</span></span><br><span class="line">    <span class="comment">//提示：</span></span><br><span class="line">    <span class="comment">//在循环外而不是循环中声明并初始化/定义对象，避免重复进行对象的构造和析构，减少开销</span></span><br><span class="line">    <span class="comment">//但可读性差</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 16轮轮函数</span></span><br><span class="line">    <span class="comment">//注意：轮数从1开始</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> wheelCount = <span class="number">1</span>; wheelCount &lt;= <span class="number">16</span>; ++wheelCount) <span class="comment">//轮数计数</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//轮密钥生成</span></span><br><span class="line">        <span class="comment">//参数：上一轮密钥（56位），轮数（从1开始），下一轮密钥（56位），轮密钥（48位）</span></span><br><span class="line">        <span class="keyword">this</span>-&gt;<span class="built_in">wheelKeyGener</span>(preKey, wheelCount, nextKey, wheelKey);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">this</span>-&gt;wheelKeys[wheelCount - <span class="number">1</span>] = wheelKey; <span class="comment">//记录轮密钥</span></span><br><span class="line">        <span class="comment">//注意：轮数从1开始，轮密钥下标从0开始</span></span><br><span class="line">        <span class="comment">//注意：记录轮密钥（48位）而不是下一轮密钥（56位）</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//注意：</span></span><br><span class="line">        <span class="comment">//循环外定义的变量可能需要更新</span></span><br><span class="line">        <span class="comment">//循环内定义的变量为局部变量，不需要更新，方便</span></span><br><span class="line">        preKey = nextKey; <span class="comment">//更新上一轮密钥 56位</span></span><br><span class="line">        nextKey = <span class="string">&quot;&quot;</span>;     <span class="comment">//更新下一轮密钥 56位</span></span><br><span class="line">        <span class="comment">//轮密钥每轮都通过下标填充，不需要更新</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//对明文：</span></span><br><span class="line">    <span class="comment">// 1.初始置换IP 64-&gt;64位</span></span><br><span class="line">    <span class="function">string <span class="title">plainTextIp</span><span class="params">(<span class="number">64</span>, <span class="string">&#x27;-&#x27;</span>)</span></span>; <span class="comment">//明文的初始置换IP   64位</span></span><br><span class="line">    <span class="comment">//注意：因为需要使用下标赋值，所以需要初始化大小以预分配空间</span></span><br><span class="line">    <span class="comment">//注意：初始化为不是0或1的字符，以验证是否成功</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>-&gt;ipTable.<span class="built_in">size</span>(); ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//置换规则：依据置换表，输入的第58位是输出的第1位-&gt;输入的第57位是输出的第0位</span></span><br><span class="line">        <span class="comment">//注意：下标从0开始</span></span><br><span class="line">        plainTextIp[i] = <span class="keyword">this</span>-&gt;plainTextASCII[<span class="keyword">this</span>-&gt;ipTable[i] - <span class="number">1</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;明文的初始置换IP：\t&quot;</span> &lt;&lt; plainTextIp &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2.轮函数F</span></span><br><span class="line">    <span class="function">string <span class="title">plainTextWheelF</span><span class="params">(<span class="string">&quot;&quot;</span>)</span></span>; <span class="comment">//明文的轮函数  64位</span></span><br><span class="line">    <span class="built_in">wheelFunc</span>(plainTextIp, keySeRep, plainTextWheelF);</span><br><span class="line">    <span class="comment">//参数：明文的初始置换IP，密钥的选择置换，明文的轮函数</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;明文的轮函数F：\t&quot;</span> &lt;&lt; plainTextWheelF &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 3.左右置换/轮函数F的第16轮不进行左右置换 64-&gt;64位</span></span><br><span class="line">    <span class="function">string <span class="title">left</span><span class="params">(plainTextWheelF.substr(<span class="number">0</span>, <span class="number">32</span>))</span></span>;   <span class="comment">//左 范围：0~31</span></span><br><span class="line">    <span class="function">string <span class="title">right</span><span class="params">(plainTextWheelF.substr(<span class="number">32</span>, <span class="number">32</span>))</span></span>; <span class="comment">//右 范围：32~63</span></span><br><span class="line">    string plainTextLeRiRep = right + left;       <span class="comment">//明文的左右置换</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;明文的左右置换：\t&quot;</span> &lt;&lt; plainTextLeRiRep &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 4.逆初始置换IP-1 64-&gt;64位</span></span><br><span class="line">    <span class="function">string <span class="title">plainTextIIp</span><span class="params">(<span class="number">64</span>, <span class="string">&#x27;-&#x27;</span>)</span></span>; <span class="comment">//明文的逆初始置换IP-1</span></span><br><span class="line">    <span class="comment">//注意：因为需要使用下标赋值，所以需要初始化大小以预分配空间</span></span><br><span class="line">    <span class="comment">//注意：初始化为不是0或1的字符，以验证是否成功</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>-&gt;iIpTable.<span class="built_in">size</span>(); ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//置换规则：依据置换表，输入的第40位是输出的第1位-&gt;输入的第39位是输出的第0位</span></span><br><span class="line">        <span class="comment">//注意：下标从0开始</span></span><br><span class="line">        plainTextIIp[i] = plainTextLeRiRep[<span class="keyword">this</span>-&gt;iIpTable[i] - <span class="number">1</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;明文的逆初始置换IP-1：\t&quot;</span> &lt;&lt; plainTextIIp &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>-&gt;cipherTextASCII = plainTextIIp; <span class="comment">//记录密文的ASCII码   密文的ASCII码为明文的逆初始置换IP-1</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;密文的ASCII码：\t&quot;</span> &lt;&lt; <span class="keyword">this</span>-&gt;cipherTextASCII &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    cipherTextASCII = plainTextIIp; <span class="comment">//记录结果   密文的ASCII码为明文的逆初始置换IP-1</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 8字节字符串转64位字符串 依据ASCII码</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">DES::byteStrToBitStr</span><span class="params">(<span class="type">const</span> string &amp;str, string &amp;bitStr)</span> <span class="comment">//参数：8字节字符串，64位字符串</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">const</span> <span class="type">char</span> &amp;ch : str)</span><br><span class="line">    &#123;</span><br><span class="line">        bitStr += <span class="built_in">bitset</span>&lt;<span class="number">8</span>&gt;(ch).<span class="built_in">to_string</span>(); <span class="comment">// 1字节字符转8位字符串    字符-&gt;bitset对象-&gt;字符串</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//轮密钥生成</span></span><br><span class="line"><span class="comment">//参数：上一轮密钥（56位），轮数（从1开始），下一轮密钥（56位），轮密钥（48位）</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">DES::wheelKeyGener</span><span class="params">(<span class="type">const</span> string &amp;preKey, <span class="type">const</span> <span class="type">int</span> &amp;wheelCount, string &amp;nextKey, string &amp;wheelKey)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 2.1等分上一轮密钥为上一轮左右    56-&gt;28+28位</span></span><br><span class="line">    <span class="comment">//注意：substr()的参数：开始下标，子串大小</span></span><br><span class="line">    <span class="function">string <span class="title">preLeft</span><span class="params">(preKey.substr(<span class="number">0</span>, <span class="number">28</span>))</span></span>;   <span class="comment">//上一轮左  范围：0~27</span></span><br><span class="line">    <span class="function">string <span class="title">preRight</span><span class="params">(preKey.substr(<span class="number">28</span>, <span class="number">28</span>))</span></span>; <span class="comment">//上一轮右  范围：28~55</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2.2上一轮左右各循环左移/旋转</span></span><br><span class="line">    <span class="comment">//上一轮左的左移</span></span><br><span class="line">    <span class="comment">//局部翻转</span></span><br><span class="line">    <span class="comment">//注意：轮数从1开始，下标从0开始</span></span><br><span class="line">    <span class="comment">//注意：reverse()是左闭右开区间[)</span></span><br><span class="line">    <span class="built_in">reverse</span>(preLeft.<span class="built_in">begin</span>(), preLeft.<span class="built_in">begin</span>() + <span class="keyword">this</span>-&gt;keyLeftMoveTable[wheelCount - <span class="number">1</span>]); <span class="comment">//翻转[0,1)或[0,2)</span></span><br><span class="line">    <span class="built_in">reverse</span>(preLeft.<span class="built_in">begin</span>() + <span class="keyword">this</span>-&gt;keyLeftMoveTable[wheelCount - <span class="number">1</span>], preLeft.<span class="built_in">end</span>());   <span class="comment">//翻转[1,n)或[2,n)</span></span><br><span class="line">    <span class="comment">//整体翻转</span></span><br><span class="line">    <span class="built_in">reverse</span>(preLeft.<span class="built_in">begin</span>(), preLeft.<span class="built_in">end</span>()); <span class="comment">//翻转[0,n)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//上一轮右的左移位</span></span><br><span class="line">    <span class="comment">//局部翻转</span></span><br><span class="line">    <span class="comment">//注意：轮数从1开始，下标从0开始</span></span><br><span class="line">    <span class="comment">//注意：reverse()是左闭右开区间[)</span></span><br><span class="line">    <span class="built_in">reverse</span>(preRight.<span class="built_in">begin</span>(), preRight.<span class="built_in">begin</span>() + <span class="keyword">this</span>-&gt;keyLeftMoveTable[wheelCount - <span class="number">1</span>]); <span class="comment">//翻转[0,1)或[0,2)</span></span><br><span class="line">    <span class="built_in">reverse</span>(preRight.<span class="built_in">begin</span>() + <span class="keyword">this</span>-&gt;keyLeftMoveTable[wheelCount - <span class="number">1</span>], preRight.<span class="built_in">end</span>());   <span class="comment">//翻转[1,n)或[2,n)</span></span><br><span class="line">    <span class="comment">//整体翻转</span></span><br><span class="line">    <span class="built_in">reverse</span>(preRight.<span class="built_in">begin</span>(), preRight.<span class="built_in">end</span>()); <span class="comment">//翻转[0,n)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2.3拼接上一轮左右得下一轮密钥    28+28-&gt;56位</span></span><br><span class="line">    nextKey = preLeft + preRight;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2.4下一轮密钥的压缩置换得轮密钥    56-&gt;48位</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>-&gt;keyComRepTable.<span class="built_in">size</span>(); ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//置换规则：依据置换表，输入的第14位是输出的第1位-&gt;输入的第13位是输出的第0位</span></span><br><span class="line">        <span class="comment">//注意：下标从0开始</span></span><br><span class="line">        wheelKey[i] = nextKey[<span class="keyword">this</span>-&gt;keyComRepTable[i] - <span class="number">1</span>];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//轮函数F</span></span><br><span class="line"><span class="comment">//注意：忘记加类作用域...</span></span><br><span class="line"><span class="comment">//参数：文本的初始置换IP，密钥的选择置换，文本的轮函数F</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">DES::wheelFunc</span><span class="params">(<span class="type">const</span> string &amp;textIp, <span class="type">const</span> string &amp;keySeRep, string &amp;textWheelF)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">string <span class="title">preText</span><span class="params">(textIp)</span></span>; <span class="comment">//上一轮文本    64位</span></span><br><span class="line">    <span class="comment">//提示：</span></span><br><span class="line">    <span class="comment">//在循环外而不是循环中声明并初始化/定义对象，避免重复进行对象的构造和析构，减少开销</span></span><br><span class="line">    <span class="comment">//但可读性差</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 16轮轮函数</span></span><br><span class="line">    <span class="comment">//注意：轮数从1开始</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> wheelCount = <span class="number">1</span>; wheelCount &lt;= <span class="number">16</span>; ++wheelCount) <span class="comment">//轮数计数</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">// 2.1等分上一轮文本为上一轮左右    64-&gt;32+32位</span></span><br><span class="line">        <span class="comment">//注意：substr()的参数：开始下标，子串大小</span></span><br><span class="line">        <span class="function">string <span class="title">preLeft</span><span class="params">(preText.substr(<span class="number">0</span>, <span class="number">32</span>))</span></span>;   <span class="comment">//上一轮左   范围：0~31</span></span><br><span class="line">        <span class="function">string <span class="title">preRight</span><span class="params">(preText.substr(<span class="number">32</span>, <span class="number">32</span>))</span></span>; <span class="comment">//上一轮右 范围：32~63</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 2.2下一轮左为上一轮右    32-&gt;32位</span></span><br><span class="line">        <span class="function">string <span class="title">nextLeft</span><span class="params">(preRight)</span></span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 2.3.1下一轮右：上一轮右扩展置换E 32-&gt;48位</span></span><br><span class="line">        <span class="function">string <span class="title">preRightERep</span><span class="params">(<span class="number">48</span>, <span class="string">&#x27;-&#x27;</span>)</span></span>; <span class="comment">//上一轮右的扩展置换E</span></span><br><span class="line">        <span class="comment">//注意：因为需要使用下标赋值，所以需要初始化大小以预分配空间</span></span><br><span class="line">        <span class="comment">//注意：初始化为不是0或1的字符，以验证上一轮右的扩展置换E是否成功</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>-&gt;eRepTable.<span class="built_in">size</span>(); ++i)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">//置换规则：依据置换表，输入的第32位是输出的第1位-&gt;输入的第31位是输出的第0位</span></span><br><span class="line">            <span class="comment">//注意：下标从0开始</span></span><br><span class="line">            preRightERep[i] = preRight[<span class="keyword">this</span>-&gt;eRepTable[i] - <span class="number">1</span>];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 2.3.2下一轮右：上一轮右的扩展置换E（48位）与轮密钥（48位）异或 48w-&gt;48位</span></span><br><span class="line">        <span class="function">string <span class="title">preRightXor</span><span class="params">(<span class="string">&quot;&quot;</span>)</span></span>; <span class="comment">//上一轮右的异或</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; preRightERep.<span class="built_in">size</span>(); ++i)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">//注意：轮数计数从1开始，轮密钥的下标从0开始</span></span><br><span class="line">            <span class="keyword">if</span> (preRightERep[i] == <span class="keyword">this</span>-&gt;wheelKeys[wheelCount - <span class="number">1</span>][i]) <span class="comment">//相同为0</span></span><br><span class="line">            &#123;</span><br><span class="line">                preRightXor += <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> <span class="comment">//不同为&#x27;1&#x27;</span></span><br><span class="line">            &#123;</span><br><span class="line">                preRightXor += <span class="string">&#x27;1&#x27;</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 2.3.4下一轮右：上一轮右的异或经S盒选择代替   48-&gt;32位</span></span><br><span class="line">        <span class="function">string <span class="title">preRightSBoxSeRep</span><span class="params">(<span class="string">&quot;&quot;</span>)</span></span>; <span class="comment">//上一轮右的S盒选择代替</span></span><br><span class="line">        <span class="type">int</span> sBoxCount = <span class="number">0</span>;            <span class="comment">// S盒计数</span></span><br><span class="line">        <span class="comment">//注意：S盒计数从0开始</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; preRightXor.<span class="built_in">size</span>();) <span class="comment">// i循环范围：0~47</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="function">string <span class="title">sBoxGroup</span><span class="params">(preRightXor.substr(i, <span class="number">6</span>))</span></span>; <span class="comment">// S盒组   一组从i开始取6位数</span></span><br><span class="line">            <span class="comment">//注意：substr()参数：开始下标，子串长度</span></span><br><span class="line"></span><br><span class="line">            string rowStr&#123;sBoxGroup[<span class="number">0</span>], sBoxGroup[<span class="number">5</span>]&#125;; <span class="comment">//行字符串：首尾2位</span></span><br><span class="line">            <span class="comment">//注意：初始化方式</span></span><br><span class="line">            <span class="type">int</span> rowNumber = <span class="built_in">stoi</span>(rowStr, <span class="number">0</span>, <span class="number">2</span>); <span class="comment">//行号</span></span><br><span class="line">            <span class="comment">//注意：stoi()参数：字符串，开始下标，字符串的基数</span></span><br><span class="line"></span><br><span class="line">            <span class="function">string <span class="title">colStr</span><span class="params">(sBoxGroup.substr(<span class="number">1</span>, <span class="number">4</span>))</span></span>; <span class="comment">//列字符串：中间4位</span></span><br><span class="line">            <span class="comment">//注意：初始化方式</span></span><br><span class="line">            <span class="type">int</span> colNumber = <span class="built_in">stoi</span>(colStr, <span class="number">0</span>, <span class="number">2</span>); <span class="comment">//列号</span></span><br><span class="line">            <span class="comment">//注意：stoi()参数：字符串，开始下标，字符串的基数</span></span><br><span class="line"></span><br><span class="line">            <span class="type">int</span> pos = rowNumber * <span class="number">16</span> + colNumber; <span class="comment">//位置：第rowNumber行第colNumber列</span></span><br><span class="line">            <span class="comment">//注意：行数从0开始，列数从0开始</span></span><br><span class="line">            <span class="type">char</span> sBoxNumber = <span class="keyword">this</span>-&gt;sBoxSeRepTable[sBoxCount][pos]; <span class="comment">// S盒数</span></span><br><span class="line">            <span class="comment">//注意：</span></span><br><span class="line">            <span class="comment">// S盒计数从0开始   第0盒-&gt;S1</span></span><br><span class="line">            <span class="comment">// S盒数是char数据类型</span></span><br><span class="line"></span><br><span class="line">            preRightSBoxSeRep += <span class="built_in">bitset</span>&lt;<span class="number">4</span>&gt;(sBoxNumber).<span class="built_in">to_string</span>(); <span class="comment">// 1字节字符转4位字符串 字符-&gt;bitset对象-&gt;字符串</span></span><br><span class="line"></span><br><span class="line">            sBoxCount += <span class="number">1</span>; <span class="comment">// S盒计数+1</span></span><br><span class="line">            i += <span class="number">6</span>;         <span class="comment">//循环i+6</span></span><br><span class="line">            <span class="comment">// 注意：i变化范围：0~5，6~11，12~17，18...</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 2.3.5下一轮右：上一轮右置换P 32-&gt;32位</span></span><br><span class="line">        <span class="function">string <span class="title">preRightPRep</span><span class="params">(<span class="number">32</span>, <span class="string">&#x27;-&#x27;</span>)</span></span>; <span class="comment">//上一轮右的置换P</span></span><br><span class="line">        <span class="comment">//注意：因为需要使用下标赋值，所以需要初始化大小以预分配空间</span></span><br><span class="line">        <span class="comment">//注意：初始化为不是0或1的字符，以验证是否成功</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>-&gt;pRepTable.<span class="built_in">size</span>(); ++i)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">//置换规则：依据置换表，输入的第16位是输出的第1位-&gt;输入的第15位是输出的第0位</span></span><br><span class="line">            <span class="comment">//注意：下标从0开始</span></span><br><span class="line">            preRightPRep[i] = preRightSBoxSeRep[<span class="keyword">this</span>-&gt;pRepTable[i] - <span class="number">1</span>];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 2.3.6下一轮右：上一轮右与上一轮左异或    32-&gt;32位</span></span><br><span class="line">        <span class="function">string <span class="title">preRightXor2</span><span class="params">(<span class="string">&quot;&quot;</span>)</span></span>; <span class="comment">//下一轮右的异或2</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; preLeft.<span class="built_in">size</span>(); ++i)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span> (preRightPRep[i] == preLeft[i]) <span class="comment">//相同为0</span></span><br><span class="line">            &#123;</span><br><span class="line">                preRightXor2 += <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> <span class="comment">//不同为&#x27;1&#x27;</span></span><br><span class="line">            &#123;</span><br><span class="line">                preRightXor2 += <span class="string">&#x27;1&#x27;</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function">string <span class="title">nextRight</span><span class="params">(preRightXor2)</span></span>; <span class="comment">//下一轮右</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//注意：</span></span><br><span class="line">        <span class="comment">//循环外定义的变量可能需要更新</span></span><br><span class="line">        <span class="comment">//循环内定义的变量为局部变量，不需要更新，方便</span></span><br><span class="line">        preText = nextLeft + nextRight; <span class="comment">//更新上一轮文本</span></span><br><span class="line"></span><br><span class="line">        cout &lt;&lt; <span class="string">&quot;明/密文的第&quot;</span> &lt;&lt; wheelCount &lt;&lt; <span class="string">&quot;轮输出：\t&quot;</span> &lt;&lt; preText &lt;&lt; endl;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    textWheelF = preText; <span class="comment">//文本的轮函数F为上一轮文本</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//解密</span></span><br><span class="line"><span class="comment">//注意过程：</span></span><br><span class="line"><span class="comment">// 1.初始置换IP</span></span><br><span class="line"><span class="comment">// 2.轮函数F</span></span><br><span class="line"><span class="comment">// 3.左右置换</span></span><br><span class="line"><span class="comment">// 4.逆初始置换IP-1</span></span><br><span class="line"><span class="comment">//提示：因为通过重构解耦合，直接复用加密函数代码，修改变量名称和注释即可</span></span><br><span class="line"><span class="comment">//注意：</span></span><br><span class="line"><span class="comment">//加密的轮函数F中，使用轮密钥K1，K2...K16，轮函数下标0，1...15</span></span><br><span class="line"><span class="comment">//解密的轮函数F中，使用轮密钥K16，K15...K1，轮函数下标15，14...1</span></span><br><span class="line"><span class="comment">//所以，解密的轮密钥生成中，反向记录轮密钥，即可复用相同的轮函数函数</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">DES::decrypt</span><span class="params">(<span class="type">const</span> string &amp;cipherTextASCII, <span class="type">const</span> string &amp;key, string &amp;plainTextASCII)</span> <span class="comment">//参数：密文的ASCII码，密钥，明文的ASCII码</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;解密过程：————————————————————&quot;</span> &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//对密文和密钥：</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;密钥：\t&quot;</span> &lt;&lt; key &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>-&gt;cipherTextASCII = cipherTextASCII; <span class="comment">//记录密文的ASCII码</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;密文的ASCII码：\t&quot;</span> &lt;&lt; <span class="keyword">this</span>-&gt;cipherTextASCII &lt;&lt; endl;</span><br><span class="line">    <span class="comment">// 0.初始化为二进制  8字节-&gt;64位</span></span><br><span class="line">    <span class="function">string <span class="title">keyASCII</span><span class="params">(<span class="string">&quot;&quot;</span>)</span></span>;                  <span class="comment">//密钥的ASCII码   64位</span></span><br><span class="line">    <span class="keyword">this</span>-&gt;<span class="built_in">byteStrToBitStr</span>(key, keyASCII); <span class="comment">// 8字节字符串转64位字符串 依据ASCII码</span></span><br><span class="line">    <span class="keyword">this</span>-&gt;keyASCII = keyASCII;            <span class="comment">//记录密钥的ASCII码</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;密钥的ASCII码：\t&quot;</span> &lt;&lt; <span class="keyword">this</span>-&gt;keyASCII &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//对密钥：</span></span><br><span class="line">    <span class="comment">// 1.密钥的选择置换 64-&gt;56位</span></span><br><span class="line">    <span class="function">string <span class="title">keySeRep</span><span class="params">(<span class="number">56</span>, <span class="string">&#x27;-&#x27;</span>)</span></span>; <span class="comment">//密钥的选择置换   56位</span></span><br><span class="line">    <span class="comment">//注意：因为需要使用下标赋值，所以需要初始化大小以预分配空间</span></span><br><span class="line">    <span class="comment">//注意：初始化为不是0或1的字符，以验证是否成功</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>-&gt;keySeRepTable.<span class="built_in">size</span>(); ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//置换规则：依据置换表，输入的第57位是输出的第1位-&gt;输入的第56位是输出的第0位</span></span><br><span class="line">        <span class="comment">//注意：下标从0开始</span></span><br><span class="line">        keySeRep[i] = <span class="keyword">this</span>-&gt;keyASCII[<span class="keyword">this</span>-&gt;keySeRepTable[i] - <span class="number">1</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;密钥的选择置换：\t&quot;</span> &lt;&lt; keySeRep &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2.轮密钥生成</span></span><br><span class="line">    <span class="comment">//注意：预先生成</span></span><br><span class="line">    <span class="function">string <span class="title">preKey</span><span class="params">(keySeRep)</span></span>;  <span class="comment">//上一轮密钥 56位</span></span><br><span class="line">    <span class="function">string <span class="title">nextKey</span><span class="params">(<span class="string">&quot;&quot;</span>)</span></span>;       <span class="comment">//下一轮密钥 56位</span></span><br><span class="line">    <span class="function">string <span class="title">wheelKey</span><span class="params">(<span class="number">48</span>, <span class="string">&#x27;-&#x27;</span>)</span></span>; <span class="comment">//轮密钥 48位</span></span><br><span class="line">    <span class="comment">//注意：因为需要使用下标赋值，所以需要初始化大小以预分配空间</span></span><br><span class="line">    <span class="comment">//注意：初始化为不是0或1的字符，以验证是否成功</span></span><br><span class="line">    <span class="comment">//提示：</span></span><br><span class="line">    <span class="comment">//在循环外而不是循环中声明并初始化/定义对象，避免重复进行对象的构造和析构，减少开销</span></span><br><span class="line">    <span class="comment">//但可读性差</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 16轮轮函数</span></span><br><span class="line">    <span class="comment">//注意：轮数从1开始</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> wheelCount = <span class="number">1</span>; wheelCount &lt;= <span class="number">16</span>; ++wheelCount) <span class="comment">//轮数计数</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//轮密钥生成</span></span><br><span class="line">        <span class="comment">//参数：上一轮密钥（56位），轮数（从1开始），下一轮密钥（56位），轮密钥（48位）</span></span><br><span class="line">        <span class="keyword">this</span>-&gt;<span class="built_in">wheelKeyGener</span>(preKey, wheelCount, nextKey, wheelKey);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//记录轮密钥</span></span><br><span class="line">        <span class="keyword">this</span>-&gt;wheelKeys[<span class="number">16</span> - wheelCount] = wheelKey;</span><br><span class="line">        <span class="comment">//注意：轮数从1开始，轮密钥下标从15开始</span></span><br><span class="line">        <span class="comment">//注意：记录轮密钥（48位）而不是下一轮密钥（56位）</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//注意：</span></span><br><span class="line">        <span class="comment">//循环外定义的变量可能需要更新</span></span><br><span class="line">        <span class="comment">//循环内定义的变量为局部变量，不需要更新，方便</span></span><br><span class="line">        preKey = nextKey; <span class="comment">//更新上一轮密钥 56位</span></span><br><span class="line">        nextKey = <span class="string">&quot;&quot;</span>;     <span class="comment">//更新下一轮密钥 56位</span></span><br><span class="line">        <span class="comment">//轮密钥每轮都通过下标填充，不需要更新</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//对密文：</span></span><br><span class="line">    <span class="comment">// 1.初始置换IP 64-&gt;64位</span></span><br><span class="line">    <span class="function">string <span class="title">cipherTextIp</span><span class="params">(<span class="number">64</span>, <span class="string">&#x27;-&#x27;</span>)</span></span>; <span class="comment">//密文的初始置换IP   64位</span></span><br><span class="line">    <span class="comment">//注意：因为需要使用下标赋值，所以需要初始化大小以预分配空间</span></span><br><span class="line">    <span class="comment">//注意：初始化为不是0或1的字符，以验证是否成功</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>-&gt;ipTable.<span class="built_in">size</span>(); ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//置换规则：依据置换表，输入的第58位是输出的第1位-&gt;输入的第57位是输出的第0位</span></span><br><span class="line">        <span class="comment">//注意：下标从0开始</span></span><br><span class="line">        cipherTextIp[i] = <span class="keyword">this</span>-&gt;cipherTextASCII[<span class="keyword">this</span>-&gt;ipTable[i] - <span class="number">1</span>];</span><br><span class="line">        <span class="comment">//注意：忘记修改this-&gt;plain为cipher了...</span></span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;密文的初始置换IP：\t&quot;</span> &lt;&lt; cipherTextIp &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2.轮函数F</span></span><br><span class="line">    <span class="function">string <span class="title">cipherTextWheelF</span><span class="params">(<span class="string">&quot;&quot;</span>)</span></span>; <span class="comment">//密文的轮函数  64位</span></span><br><span class="line">    <span class="built_in">wheelFunc</span>(cipherTextIp, keySeRep, cipherTextWheelF);</span><br><span class="line">    <span class="comment">//参数：密文的初始置换IP，密钥的选择置换，密文的轮函数</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;密文的轮函数F：\t&quot;</span> &lt;&lt; cipherTextWheelF &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 3.左右置换/轮函数F的第16轮不进行左右置换 64-&gt;64位</span></span><br><span class="line">    <span class="function">string <span class="title">left</span><span class="params">(cipherTextWheelF.substr(<span class="number">0</span>, <span class="number">32</span>))</span></span>;   <span class="comment">//左 范围：0~31</span></span><br><span class="line">    <span class="function">string <span class="title">right</span><span class="params">(cipherTextWheelF.substr(<span class="number">32</span>, <span class="number">32</span>))</span></span>; <span class="comment">//右 范围：32~63</span></span><br><span class="line">    string cipherTextLeRiRep = right + left;       <span class="comment">//密文的左右置换</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;密文的左右置换：\t&quot;</span> &lt;&lt; cipherTextLeRiRep &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 4.逆初始置换IP-1 64-&gt;64位</span></span><br><span class="line">    <span class="function">string <span class="title">cipherTextIIp</span><span class="params">(<span class="number">64</span>, <span class="string">&#x27;-&#x27;</span>)</span></span>; <span class="comment">//密文的逆初始置换IP-1</span></span><br><span class="line">    <span class="comment">//注意：因为需要使用下标赋值，所以需要初始化大小以预分配空间</span></span><br><span class="line">    <span class="comment">//注意：初始化为不是0或1的字符，以验证是否成功</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>-&gt;iIpTable.<span class="built_in">size</span>(); ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//置换规则：依据置换表，输入的第40位是输出的第1位-&gt;输入的第39位是输出的第0位</span></span><br><span class="line">        <span class="comment">//注意：下标从0开始</span></span><br><span class="line">        cipherTextIIp[i] = cipherTextLeRiRep[<span class="keyword">this</span>-&gt;iIpTable[i] - <span class="number">1</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;密文的逆初始置换IP-1：\t&quot;</span> &lt;&lt; cipherTextIIp &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>-&gt;plainTextASCII = cipherTextIIp; <span class="comment">//记录明文的ASCII码   明文的ASCII码为密文的逆初始置换IP-1</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;明文的ASCII码：\t&quot;</span> &lt;&lt; <span class="keyword">this</span>-&gt;plainTextASCII &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    plainTextASCII = cipherTextIIp; <span class="comment">//记录结果   明文的ASCII码为密文的逆初始置换IP-1</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="main-cpp"><a href="#main-cpp" class="headerlink" title="main.cpp"></a>main.cpp</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//预处理指令——————————</span></span><br><span class="line"><span class="comment">//自定义头文件</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;des.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//函数声明——————————</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">compareStrBit</span><span class="params">(string str1, string str2)</span></span>; <span class="comment">//比较两字符串不同的位数</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//主函数——————————</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    DES *des = <span class="keyword">new</span> <span class="built_in">DES</span>(); <span class="comment">//数据加密标准对象</span></span><br><span class="line">    <span class="comment">//提示：</span></span><br><span class="line">    <span class="comment">//复杂类型/自定义类型可能占用空间大，堆区比栈区大，需要在堆区手动管理</span></span><br><span class="line">    <span class="comment">//手动管理：使用new创建对象，使用delete释放对象-&gt;搭配使用</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">const</span> string <span class="title">plainText</span><span class="params">(<span class="string">&quot;yezhenin&quot;</span>)</span></span>; <span class="comment">//明文</span></span><br><span class="line">    <span class="function"><span class="type">const</span> string <span class="title">key</span><span class="params">(<span class="string">&quot;91002705&quot;</span>)</span></span>;       <span class="comment">//密钥</span></span><br><span class="line">    <span class="function">string <span class="title">cipherTextASCII</span><span class="params">(<span class="string">&quot;&quot;</span>)</span></span>;         <span class="comment">//密文的ASCII码</span></span><br><span class="line">    <span class="comment">//提示：</span></span><br><span class="line">    <span class="comment">//密文的ASCII码没有必要转换为字符，因为：</span></span><br><span class="line">    <span class="comment">// 1.转换的字符无意义</span></span><br><span class="line">    <span class="comment">// 2.转换的字符可能为非打印字符</span></span><br><span class="line">    <span class="function">string <span class="title">plainTextASCII</span><span class="params">(<span class="string">&quot;&quot;</span>)</span></span>; <span class="comment">//明文的ASCII码</span></span><br><span class="line"></span><br><span class="line">    des-&gt;<span class="built_in">encrypt</span>(plainText, key, cipherTextASCII); <span class="comment">//加密</span></span><br><span class="line">    <span class="comment">//参数：明文（已知），密钥（已知），密文的ASCII码（未知）</span></span><br><span class="line"></span><br><span class="line">    des-&gt;<span class="built_in">decrypt</span>(cipherTextASCII, key, plainTextASCII); <span class="comment">//解密</span></span><br><span class="line">    <span class="comment">//参数：密文的ASCII码（已知），密钥（已知），明文的ASCII码（未知）</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//字符转ASCII码转换器网址：https://www.qqxiuzi.cn/bianma/ascii.htm</span></span><br><span class="line">    <span class="comment">// ASCII码转字符转换器网址：https://www.asciim.cn/m/tools/convert_ascii_to_string.html</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//雪崩效应测试</span></span><br><span class="line">    <span class="comment">//提示：</span></span><br><span class="line">    <span class="comment">//由设计接口：测试明文变化需要考虑到字节层面，传递明文参数；测试密文变化需要考虑到位层面，传递密文的ASCII码参数</span></span><br><span class="line">    <span class="comment">//因为加密和解密过程相似，所以变化密文进行测试</span></span><br><span class="line"></span><br><span class="line">    string cipherTextCo = <span class="string">&quot;0000110101111001101000001011100001101001111000010100000001011011&quot;</span>; <span class="comment">//密文正确码</span></span><br><span class="line">    string plainTextCo = <span class="string">&quot;0111100101100101011110100110100001100101011011100110100101101110&quot;</span>;  <span class="comment">//明文正确码</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//第一组</span></span><br><span class="line">    string cipherTextEr = cipherTextCo;           <span class="comment">//密文错误码为密文正确码</span></span><br><span class="line">    cipherTextEr[<span class="number">0</span>] = <span class="number">1</span>;                          <span class="comment">//修改1位：0-&gt;1</span></span><br><span class="line">    <span class="function">string <span class="title">plainTextEr</span><span class="params">(<span class="string">&quot;&quot;</span>)</span></span>;                       <span class="comment">//明文错误码</span></span><br><span class="line">    des-&gt;<span class="built_in">decrypt</span>(cipherTextEr, key, plainTextEr); <span class="comment">//解密</span></span><br><span class="line">    <span class="comment">//参数：密文错误码（已知），密钥（已知），明文错误码（未知）</span></span><br><span class="line">    <span class="built_in">compareStrBit</span>(plainTextCo, plainTextEr); <span class="comment">//比较两字符串不同的位数</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//第二组</span></span><br><span class="line">    cipherTextEr = cipherTextCo;                  <span class="comment">//密文错误码为密文正确码</span></span><br><span class="line">    cipherTextEr[<span class="number">0</span>] = <span class="number">1</span>;                          <span class="comment">//修改1位：0-&gt;1</span></span><br><span class="line">    cipherTextEr[<span class="number">63</span>] = <span class="number">0</span>;                         <span class="comment">//修改1位：1-&gt;0</span></span><br><span class="line">    plainTextEr = <span class="string">&quot;&quot;</span>;                             <span class="comment">//明文错误码</span></span><br><span class="line">    des-&gt;<span class="built_in">decrypt</span>(cipherTextEr, key, plainTextEr); <span class="comment">//解密</span></span><br><span class="line">    <span class="comment">//参数：密文错误码（已知），密钥（已知），明文错误码（未知）</span></span><br><span class="line">    <span class="built_in">compareStrBit</span>(plainTextCo, plainTextEr); <span class="comment">//较两字符串不同的位数</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//第三组</span></span><br><span class="line">    cipherTextEr = cipherTextCo;                  <span class="comment">//密文错误码为密文正确码</span></span><br><span class="line">    cipherTextEr[<span class="number">0</span>] = <span class="number">1</span>;                          <span class="comment">//修改1位：0-&gt;1</span></span><br><span class="line">    cipherTextEr[<span class="number">1</span>] = <span class="number">1</span>;                          <span class="comment">//修改1位：0-&gt;1</span></span><br><span class="line">    cipherTextEr[<span class="number">63</span>] = <span class="number">0</span>;                         <span class="comment">//修改1位：1-&gt;0</span></span><br><span class="line">    plainTextEr = <span class="string">&quot;&quot;</span>;                             <span class="comment">//明文错误码</span></span><br><span class="line">    des-&gt;<span class="built_in">decrypt</span>(cipherTextEr, key, plainTextEr); <span class="comment">//解密</span></span><br><span class="line">    <span class="comment">//参数：密文错误码（已知），密钥（已知），明文错误码（未知）</span></span><br><span class="line">    <span class="built_in">compareStrBit</span>(plainTextCo, plainTextEr); <span class="comment">//较两字符串不同的位数</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">delete</span> des;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//比较两字符串不同的位数</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">compareStrBit</span><span class="params">(string str1, string str2)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> diffBitCount = <span class="number">0</span>; <span class="comment">//不同位计数</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; str<span class="number">1.</span><span class="built_in">size</span>(); ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (str1[i] != str2[i])</span><br><span class="line">        &#123;</span><br><span class="line">            ++diffBitCount;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;不同位数：\t&quot;</span> &lt;&lt; diffBitCount &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<h1 id="结果"><a href="#结果" class="headerlink" title="结果"></a>结果</h1><h2 id="加密过程"><a href="#加密过程" class="headerlink" title="加密过程"></a>加密过程</h2><p><img src= "" data-lazy-src="/../img/des/989e73190b27409980f15ae2e56b5afe.png" alt="在这里插入图片描述"></p>
<hr>
<h2 id="解密过程"><a href="#解密过程" class="headerlink" title="解密过程"></a>解密过程</h2><p><img src= "" data-lazy-src="/../img/des/a1c357d0ea38425689fd7e7d8198e21a.png" alt="在这里插入图片描述"></p>
<hr>
<h2 id="雪崩效应"><a href="#雪崩效应" class="headerlink" title="雪崩效应"></a>雪崩效应</h2><ul>
<li>修改密文的1位，解密的错误明文与正确明文的不同位数为35：</li>
</ul>
<p><img src= "" data-lazy-src="/../img/des/2b7dfe4cb19242579b8cc85fa048fe10.png" alt="在这里插入图片描述"></p>
<ul>
<li>修改密文的2位，解密的错误明文与正确明文的不同位数为31：</li>
</ul>
<p><img src= "" data-lazy-src="/../img/des/9babad9d064f4cf1b1b006154b65934f.png" alt="在这里插入图片描述"></p>
<ul>
<li>修改密文的3位，解密的错误明文与正确明文的不同位数为30：</li>
</ul>
<p><img src= "" data-lazy-src="/../img/des/f1ce3d366265441c93bcf45ec203f513.png" alt="在这里插入图片描述"></p>
<p>分析：</p>
<ul>
<li><p>明文和密钥&#x2F;密文的一位发生变化会导致密文&#x2F;明文的多位发生变化</p>
</li>
<li><p>明文&#x2F;密文发生变化的位数近似为明文&#x2F;密文的二分之一（64÷2&#x3D;32位）</p>
</li>
<li><p>数据加密标准（DES）满足雪崩效应，强度高</p>
</li>
</ul>
<hr>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>网络安全中数据加密标准（DES）的C++语言描述实现。</p>
<hr>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><ul>
<li>《密码编码学与网络安全——原理与实践(第五版)》作者：William Stallings</li>
</ul>
<hr>
<h1 id="作者的话"><a href="#作者的话" class="headerlink" title="作者的话"></a>作者的话</h1><ul>
<li><strong>感谢参考资料的作者&#x2F;博主</strong></li>
<li>作者：夜悊</li>
<li>版权所有，转载请注明出处，谢谢~</li>
<li><strong>如果文章对你有帮助，请点个赞或加个粉丝吧，你的支持就是作者的动力~</strong></li>
<li>文章在描述时有疑惑的地方，请留言，定会一一耐心讨论、解答</li>
<li>文章在认识上有错误的地方, 敬请批评指正</li>
<li>望读者们都能有所收获</li>
</ul>
<hr>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="http://example.com">夜悊</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="http://example.com/2022/10/24/%E6%95%B0%E6%8D%AE%E5%8A%A0%E5%AF%86%E6%A0%87%E5%87%86%EF%BC%88DES%EF%BC%89%E7%9A%84C++%E8%AF%AD%E8%A8%80%E6%8F%8F%E8%BF%B0%E5%AE%9E%E7%8E%B0/">http://example.com/2022/10/24/数据加密标准（DES）的C++语言描述实现/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">夜悊的技术小宅</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E7%BD%91%E7%BB%9C%E5%AE%89%E5%85%A8/">网络安全</a></div><div class="post_share"><div class="social-share" data-image="/img/cover10.png" data-sites="qq,wechat,weibo"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/10/19/%E2%80%9C%E5%9B%BE%E2%80%9D%E5%AD%A6%E4%B9%A0%E6%8F%90%E7%BA%B2/" title="“图”学习提纲"><img class="cover" src= "" data-lazy-src="/img/cover19.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">“图”学习提纲</div></div></a></div><div class="next-post pull-right"><a href="/2022/11/09/RSA%E7%9A%84C++%E8%AF%AD%E8%A8%80%E6%8F%8F%E8%BF%B0%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0/" title="RSA的C++语言描述简单实现"><img class="cover" src= "" data-lazy-src="/img/cover15.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">RSA的C++语言描述简单实现</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/11/16/Diffie-Hellman%E7%9A%84C++%E8%AF%AD%E8%A8%80%E6%8F%8F%E8%BF%B0%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0/" title="Diffie-Hellman的C++语言描述简单实现"><img class="cover" src= "" data-lazy-src="/img/cover6.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-11-16</div><div class="title">Diffie-Hellman的C++语言描述简单实现</div></div></a></div><div><a href="/2022/11/09/RSA%E7%9A%84C++%E8%AF%AD%E8%A8%80%E6%8F%8F%E8%BF%B0%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0/" title="RSA的C++语言描述简单实现"><img class="cover" src= "" data-lazy-src="/img/cover15.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-11-09</div><div class="title">RSA的C++语言描述简单实现</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E4%BB%93%E5%BA%93"><span class="toc-number">2.</span> <span class="toc-text">代码仓库</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E7%89%B9%E7%82%B9"><span class="toc-number">3.</span> <span class="toc-text">代码特点</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81"><span class="toc-number">4.</span> <span class="toc-text">代码</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#des-h"><span class="toc-number">4.1.</span> <span class="toc-text">des.h</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#des-cpp"><span class="toc-number">4.2.</span> <span class="toc-text">des.cpp</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#main-cpp"><span class="toc-number">4.3.</span> <span class="toc-text">main.cpp</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BB%93%E6%9E%9C"><span class="toc-number">5.</span> <span class="toc-text">结果</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8A%A0%E5%AF%86%E8%BF%87%E7%A8%8B"><span class="toc-number">5.1.</span> <span class="toc-text">加密过程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%A7%A3%E5%AF%86%E8%BF%87%E7%A8%8B"><span class="toc-number">5.2.</span> <span class="toc-text">解密过程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%9B%AA%E5%B4%A9%E6%95%88%E5%BA%94"><span class="toc-number">5.3.</span> <span class="toc-text">雪崩效应</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">6.</span> <span class="toc-text">总结</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="toc-number">7.</span> <span class="toc-text">参考资料</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%9C%E8%80%85%E7%9A%84%E8%AF%9D"><span class="toc-number">8.</span> <span class="toc-text">作者的话</span></a></li></ol></div></div></div></div></main><footer id="footer" style="background-image: url('/img/cover10.png')"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2025 By 夜悊</div><div class="footer_custom_text"><a target="_blank" href="https://beian.miit.gov.cn/" >琼ICP备2023001225号-1</a><br><img src= "" data-lazy-src="/img/beian.png"/>&nbsp<a target="_blank" href="https://www.beian.gov.cn/portal/registerSystemInfo?recordcode=46010802001221" >琼公网安备 46010802001221号</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="translateLink" type="button" title="简繁转换">繁</button><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js?v=4.13.0"></script><script src="/js/main.js?v=4.13.0"></script><script src="/js/tw_cn.js?v=4.13.0"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.8.8/dist/lazyload.iife.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js?v=4.13.0"></script></div></div></body></html>